package discount;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import object.Discount;

import service.CommonServlet;

public class SearchDiscount extends CommonServlet {
	
	@Override
	protected void execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try
		{
			String kword = request.getParameter("kword");
			kword = "%" + kword + "%";
			String sql = "select * from discount_coupon where discountName like ?";
			PreparedStatement pstmt = mConnection.prepareStatement(sql);
			pstmt.setString(1, kword);
			ResultSet rs = pstmt.executeQuery();
			List<Discount> contents = new ArrayList<Discount>();
			while(rs.next())
			{
				Discount content = new Discount();
				content.setdId(rs.getInt(1));
				content.setContent(rs.getString(4));
				content.setPrice(rs.getInt(3));
				content.setName(rs.getString(2));
				contents.add(content);
			}
			sendObject(contents, response);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * Constructor of the object.
	 */
	public SearchDiscount() {
		super();
	}

}
